<?php
class Widget_Joomla15Crawl
{
	static $handled_url=array();
	public static function  getConfig(){
		$ret=array();
		$ret['bjdagong.com.cn']=array(
			'charset'=>'gb2312',
			'list'=>'.main_left_list_box>ul>li>a[href]',
			'next_page'=>'.page_list a:contains(下一页)',
			'map'=>array(
				'title'=>array('sizzle'=>'.content_left_box_title>h1','node'=>'value'),
				'introtext'=>array('sizzle'=>'div.content_left_box_txt','node'=>'html'),
				'publish_up'=>array('sizzle'=>'div.content_left_box_ftitle','node'=>'value','func'=>'bjdagong_com_cn_genDate')				
			),
		);
		$ret['offcn.com']=array(
					'charset'=>'gb2312',
					'list'=>'.bk_center_ul>ul>li>a',
					'next_page'=>'.bk_center_fy>p>a[href]:contains(下一页)',
					'map'=>array(
						'title'=>array('sizzle'=>'.cont_main>h1','node'=>'value'),
						'introtext'=>array('sizzle'=>'.cont_main>h3','node'=>'html'),
						'publish_up'=>array('sizzle'=>'.cont_main>h2','node'=>'value','func'=>'offcn_com_genDate')				
					),
		);
		return $ret;
	}
	public static function getData($params=array())
	{
		//Baogg_Cache::flush();
		if(!is_array($params) && !isset($params['catid']) || !$params['catid']){
			return array();
		}
		
		$catid=@$params['catid'];
		$Categories=new Joomla15_Categories();
		$rs_cat=$Categories->getOne($catid);
		$sectionid=$rs_cat[0]['section'];		
		
		$arr_config=self::mapConfig(@$params['url']);
		
		$next_page_url=@$params['url'];
		//echo '<pre>';print_r($arr_config);print_r($next_page_url);exit;
		while($arr_config && $next_page_url){
			$tmp_next_page_url=$next_page_url;
			$html_list=Baogg_File_Html::multiGet(array($next_page_url));			
			$html_list=$html_list[0];			
			//@$arr_config['charset'] && ($html_list=iconv($arr_config['charset'],'utf-8',$html_list));
			//echo '<pre>';echo ($html_list);exit;
			phpQuery::newDocumentHTML($html_list,$arr_config['charset']);
			
			//echo $html_list;
			
			$next_page_url='';
			$arr_next_page_url=pq($arr_config['next_page']);
			foreach($arr_next_page_url as $v){
				$next_page_url=$v->getAttribute('href');
				$next_page_url=Baogg_File::fixUrlName($next_page_url, $tmp_next_page_url);
			}			
			
			$arr_list=pq($arr_config['list']);
			$arr_page_url=array();
			foreach($arr_list as $list){
				($list->getAttribute('href')) && ($arr_page_url[]=Baogg_File::fixUrlName($list->getAttribute('href'), $tmp_next_page_url));
			}
			//echo '<pre>';echo ($next_page_url);print_r($arr_page_url);exit;
			$arr_content=Baogg_File_Html::multiGet($arr_page_url);
			foreach($arr_content as $content){
				//echo $content;
				
				//$content=iconv($arr_config['charset'],'utf-8',$content);
				$doc_id=phpQuery::newDocumentHTML($content,$arr_config['charset']);
				
				$row=array();
				foreach($arr_config['map'] as $col=>$col_config){					
					$col_pq=pq($col_config['sizzle'],$doc_id);
					//echo '<pre>';var_dump($col_config['sizzle']);var_dump($col_pq);
					
					$col_val='';
					foreach($col_pq as $v){
						//var_dump($v);var_dump($v->nodeValue);exit;
						if(@$col_config['node']=='value'){
							$col_val=$v->nodeValue;
						}else{
							$col_val=$v->ownerDocument->saveXML($v);
						}
						break;
					}		
							
					
					
					if(isset($col_config['func'])){
						if(is_string($col_config['func'])){
							$col_val=$col_config['func']($col_val);
						}else if(is_array($col_config['func'])){
							foreach((array)$col_config['func'] as $func){
								$col_val=$func($col_val);
							}
						}
					}
					$row[$col]=$col_val;
				}
				
				
				
				$row['catid']=$catid;
				$row['state']=1;
				$row['sectionid']=$sectionid;
				$row['created']=date('Y-m-d H:i:s');
				
				//echo '<pre>';print_r($row);exit;
				
				$Joomla15_content=new Joomla15_Content();
				if(!$Joomla15_content->getSameRow($row)){
					$key=$Joomla15_content->insert($row);				
					$ContentPage=new Joomla15_ContentFrontpage();
					$ContentPage->insert(array('content_id'=>$key,'ordering'=>'1'));
				}
				
			}
			//echo '<pre>';print_r($arr_content);exit;
		}
		



		return $row;
	}
	//convert crawl url to config url
	public static function mapConfig($url=''){
		if(!$url){
			return array();
		}
		$arr_config=self::getConfig();
		
		$arr_url=parse_url($url);
		$arr_path=array_filter(explode('/',$arr_url['path']));

		$key=$arr_url['host'].($arr_path?'_'.implode('_',$arr_path):'');
		$flag=isset($arr_config[$key]);
		while(!$flag){
			//echo $key.'<br />';
			if($arr_path){
				array_pop($arr_path);
			}else if(strpos($arr_url['host'],'.')){
					
				$arr_url['host']=substr($arr_url['host'],strpos($arr_url['host'],'.')+1);
			}else{
				break;
			}
			$key=$arr_url['host'].($arr_path?'_'.implode('_',$arr_path):'');
			$flag=isset($arr_config[$key]);
		}
		if($flag){
			//header("Content-type: text/html; charset={$arr_config[$key]}");
			$file_name=BAOGG_ROOT."config/joomla15_crawl/$key.php";
			if(is_file($file_name)){
				Zend_Loader::loadFile($file_name,null,true);
			}
			return $arr_config[$key];
		}else{
			return $flag;
		}

	}
	
	public static function getCategories(){
		$rs=array();
		$Section=new Joomla15_Sections();
		$rs_section=$Section->getList(array('published'=>1),array("sort"=>"ordering","dir"=>"asc"));
		$Category=new Joomla15_Categories();
		$rs_category=$Category->getList(array('published'=>1),array("sort"=>"ordering","dir"=>"asc"));
		
		foreach($rs_section as $v){
			$rs[0][$v['id']]=$v['title'];
		}
		foreach($rs_category as $v){
			$rs[$v['section']][$v['id']]=$v['title'];
		}
		return $rs;
	}
}

?>